package com.gopay.bis.service.bank.impl.ebank;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.TransUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.BobjB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bobj.ebank.BobjB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

public class BobjB2cBankServiceImpl implements BankInteractRemoteService {

	@Resource(name = "bobjB2cManager")
	private BobjB2cManager bobjB2cManager;
	

	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		try {
			BobjB2cMessage messageB2C = new BobjB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			messageB2C.setSpecial("1");
			messageB2C.setOrderAmt(TransUtils.amountConvert2Fen(base.getAmount().toString()));
			messageB2C = (BobjB2cMessage) bobjB2cManager.sign(messageB2C);
			base.setBankOrderId(base.getOrderId());
			result.setDefaultModel(messageB2C);
			result.setModel("res",messageB2C);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}

	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		try {
			// B2C验签
			BobjB2cMessage messageB2C = new BobjB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			messageB2C = (BobjB2cMessage) bobjB2cManager.verify(messageB2C);
			messageB2C.setBankOrderId(messageB2C.getOrderNum());
			messageB2C.setOrderId(messageB2C.getOrderNum());
			if(messageB2C.getOrderAmt()!=null && !messageB2C.getOrderAmt().equals("")){
				messageB2C.setAmount(new BigDecimal(TransUtils.amountConvert2Yuan(messageB2C.getOrderAmt())));
			}
			result.setSuccess(true);
			result.setModel("res", messageB2C);
			result.setDefaultModel(messageB2C != null ? true : false);

			if(messageB2C.getSpecial().equals(BankConstants.BIZ_PAY_TYPE)){
			// 验签成功入账
			if (!base.isFront() && result.isSuccess() && (Boolean)result.getDefaultModel()) {
			Map<String, String> param = new HashMap<String, String>();
			if (messageB2C.getRetCode().equals("0000")) {
				// 通知后台地址入账					
				param.put("orderId", messageB2C.getOrderId());
				param.put("amt", messageB2C.getAmount().toString());
				param.put("cardType", "Unknown");
				param.put("notifyNum", "1");
				param.put("payStatus", "00");
				param.put("retCode", "00");
				param.put("retMsg", "验签成功");
				param.put("signData", messageB2C.getSignMessage());
				param.put("isFront", String.valueOf(base.isFront()));
				result.setModel("isNotifyCashier", param);
				result.setOrderStatus(true);
			} else {
				// 通知后台地址该订单失败
				param.put("orderId", messageB2C.getOrderId());
				param.put("amt", messageB2C.getAmount().toString());
				param.put("cardType", "Unknown");
				param.put("notifyNum", "1");
				param.put("payStatus", "01");
				param.put("retCode", "01");
				param.put("retMsg", messageB2C.getErrMsg());
				param.put("signData", messageB2C.getSignMessage());
				param.put("isFront", String.valueOf(base.isFront()));
				result.setModel("isNotifyCashier", param);
				result.setOrderStatus(false);
			 }
			}
			}
		
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
		return result;
	}

	@Override
	public Result query(BaseMessage base) {
		Result result = new Result(false);
		try {
			BobjB2cMessage messageB2C = new BobjB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			messageB2C.setSpecial("2");
			BobjB2cMessage res = (BobjB2cMessage) bobjB2cManager.query(messageB2C);
			result.setDefaultModel(res);
			result.setSuccess(true);
			if(BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())){
				Map<String, String> param = new HashMap<String, String>();
				param.put("orderId", messageB2C.getOrderId());
				param.put("amount", messageB2C.getOrderAmt());
				param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
				param.put("returnMessage", "对账完成，等待银行异步通知，对账结果请点击查看");
				param.put("tradeState", BisPayConstants.TRADESTATE_SPECIAL);
				result.setModel("queryResult", param);
			}
				
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
		return result;
	}

	@Override
	public Result refund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result dayend(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
